<?php
defined('IN_IXDCW') or exit('Access Denied');
$menus = array (
    array('商户列表', '?moduleid='.$moduleid.'&file='.$file),
    array('未审核', '?moduleid='.$moduleid.'&file='.$file.'&unaudited=1'),
    array('已拒绝', '?moduleid='.$moduleid.'&file='.$file.'&refuse=1'),
);
$do = new Business();
$businessDb = Db('business');
$businessDataDb = Db('businessData');
$businessAuthDb = Db('businessAuth');
$this_forward = '?moduleid='.$moduleid.'&file='.$file;

if(in_array($action, array(''))) {
	$sfields = array('商户名称');
	$dfields = array('b.shopname');

	isset($fields) && isset($dfields[$fields]) or $fields = 0;

	$fields_select = dselect($sfields, 'fields', '', $fields);

	$pagesize = !empty($psize) ? intval($psize) : 20;

	$condition = '';
	$order = ' order by ';
	if($kw) {
		if($kwType == 'shopname') {
			$condition .= " AND b.shopname LIKE '%$kw%'";
		}
		if ($kwType == 'username') {
			$memberDb = Db('member');
			$user = $memberDb->getByUsername($kw);
			if (!$user) {
				msg('不正确的用户名');
			}
			$condition .= " and b.userid={$user['userid']}";
		}
	}
	if ($unaudited) {
		$condition .= ' AND (a.verify=0 or a.license_status !=3) ';
	}
	if ($refuse) {
		$condition .= ' AND (b.status=1) ';
	}
	if ($storetype == 'mainstore') {
		$condition .= ' AND b.mainstore=1';
	}
	if ($storetype == 'branchstore') {
		$condition .= ' AND b.mainstore=0';
	}

	switch($orderType) {
		case 'itemidAsc':
			$order .= ' b.itemid ASC ';
			break;
		default:
			$order .= ' b.itemid DESC ';
			break;
	}

}
switch($action) {
	case 'setLicenseStatus':
		if ($licenseStatus == 3) {
			$businessAuthDb->updateLicenseStatus($aUserid, $licenseStatus);
			exit('营业执照验证通过!');
		} else if($licenseStatus == 2){
			$businessAuthDb->updateLicenseStatus($aUserid, $licenseStatus);
			exit('营业执照验证未审核!');
		} else if($licenseStatus == 1){
			$businessAuthDb->updateLicenseStatus($aUserid, $licenseStatus);
			exit('营业执照验证拒绝!');
		}
		break;
	case 'setInfoStatus':
		if ($verify == 3) {
			$businessDb->updateBusinessStatus($itemid, 3);
			exit('信息验证通过!');
		} else if($verify==2){
			$businessDb->updateBusinessStatus($itemid, 2);
			exit('信息验证放入待审核状态!');
		} else{
			$businessDb->updateBusinessStatus($itemid, 1);
			exit('信息验证拒绝!');
		}
		break;
	case 'delete':
		$itemid or msg(errmsg);
		$do->delete($itemid);
		dheader($this_forward);
		break;
	case 'view':
		$itemid or msg(errmsg);
		$business = $businessDb->getByItemid($itemid);
		$businessAuth = $businessAuthDb->getByUserid($business['userid']);
		if ($business['mainstore']) {
			$businessData = $businessDataDb->getByItemid($itemid);
		}
		include tpl('business_view', $module);
		break;
	default:
		$num = $businessDb->getCount($condition);
		$pn = new Pagination();
		$pn->recordsPerPage($pagesize);
		$pn->records($num);
		$condition .= $order;
		$condition .= " LIMIT " . ($pn->getPage() - 1) * $pagesize . ',' . $pagesize;
		$lists = $businessDb->getList($condition);
		include tpl('business', $module);
	break;
}